<?php

/**
 *      
 * 
 *  @author          Edmandie Samonte        (edmandie.samonte@gmail.com)
 *  @copyright       2011 
 * 
 */
class Employee extends CI_Controller {
    
    function add(){
        
        $this->load->library('session');
        
        if($this->session->userdata('logged_in') !== 'true'){
            header('Location: ' . base_url());
        }
        $data = array();
        $this->load->database();
        $data['message'] = 'Hello';
        if($this->input->post('data_on') == 'true'){
            $data['eID'] = $this->_save();
            $data['step'] = $this->input->post('step') + 1;
            $data['post'] = $this->input->post(NULL,FALSE);
        }else{
            $data['step'] = '1';
        }
        if($data['step']=='5'){
            header('Location: ' . site_url('dashboard'));
        }
        $data['positions'] = $this->db->get('positions')->result();
        $this->load->helper('HTML');
        $this->load->view('add',$data);
    }
    
    function _save(){
        $post = $this->input->post(NULL,FALSE);
        if($post['step'] == '1'){
            if($post['position'] == 'O'){
                $this->db->insert('positions',array(
                    'posID'=>NULL,
                    'posName'=>$post['position_name'],
                    'isAdmin'=>((isset($post['is_admin']))? 'T':'F')
                ));
                $post['position'] = $this->db->insert_id();
            }
            $this->db->insert('employee',array(
                'eID' => NULL,
                'eFirstName'=>$post['first_name'],
                'eMiddleName'=>$post['middle_name'],
                'eLastName'=>$post['last_name'],
                'eBDate'=>$post['birthdate'],
                'eAddress'=>$post['address'],
                'eGender'=>$post['gender'],
                'ePosition'=>$post['position'],
                'eContactNumber'=>$post['contact_number'],
                'eEmail'=>$post['email'],
                'eSkills'=>$post['skills'],
                'eStatus'=>"Employed",
                'ePictureURL'=>"anonymous.jpg"
            ));
            $eID = $this->db->insert_id();
            if(isset($post['is_admin']) || strlen($post['password']) > 4 ){
                $this->db->insert('authentication',array(
                    'eID'=>$this->db->insert_id(),
                    'auth_password'=>$post['password']
                ));
            }
            return $eID;
        }elseif($post['step'] == '2'){
            if(!isset($post['skip'])){
                if($_FILES['picture']['error'] > 0){

                }else{
                    move_uploaded_file($_FILES['picture']['tmp_name'], 
                            "rsrc/images/profile_pic/".$post['eID'] . "." . substr(strrchr($_FILES['picture']['name'],'.'),1));
                    $this->db->where('eID',$post['eID']);
                    $this->db->update('employee',array(
                        'ePictureURL'=>$post['eID'] . "." . substr(strrchr($_FILES['picture']['name'],'.'),1)
                    ));
                }
            }
            return $post['eID'];
        }elseif($post['step'] == '3'){
            return $post['eID'];
        }elseif($post['step'] == '4'){
            $data = array(
                'ecID'=>NULL,
                'eID'=>$post['eID'],
                'ecName'=>$post['ecName'],
                'ecAddress'=>$post['ecAddress'],
                'ecContactNumber'=>$post['ecContactNumber'],
                'ecRemark'=>$post['ecRemarks']
            );
            $this->db->insert('employeeemergencycontact',$data);
            return $post['eID'];
        }
    }
    
    function _edit_save(){
        $post = $this->input->post(NULL,FALSE);
        if($post['step'] == '1'){
            if($post['position'] == 'O'){
                $this->db->insert('positions',array(
                    'posID'=>NULL,
                    'posName'=>$post['position_name'],
                    'isAdmin'=>((isset($post['is_admin']))? 'T':'F')
                ));
                $post['position'] = $this->db->insert_id();
            }
            $this->db->where('eID',$post['eID']);
            $this->db->update('employee',array(
                'eFirstName'=>$post['first_name'],
                'eMiddleName'=>$post['middle_name'],
                'eLastName'=>$post['last_name'],
                'eBDate'=>$post['birthdate'],
                'eAddress'=>$post['address'],
                'eGender'=>$post['gender'],
                'ePosition'=>$post['position'],
                'eContactNumber'=>$post['contact_number'],
                'eEmail'=>$post['email'],
                'eSkills'=>$post['skills'],
                'eStatus'=>$post['status']
            ));
            $eID = $post['eID'];
            if(isset($post['is_admin']) || strlen($post['password']) > 4 ){
                $this->db->where('eID',$post['eID']);
                $this->db->update('authentication',array(
                    'auth_password'=>$post['password']
                ));
            }
            return $eID;
        }elseif($post['step'] == '2'){
            if(!isset($post['skip'])){
                if($_FILES['picture']['error'] > 0){

                }else{
                    move_uploaded_file($_FILES['picture']['tmp_name'], 
                            "rsrc/images/profile_pic/".$post['eID'] . "." . substr(strrchr($_FILES['picture']['name'],'.'),1));
                    $this->db->where('eID',$post['eID']);
                    $this->db->update('employee',array(
                        'ePictureURL'=>$post['eID'] . "." . substr(strrchr($_FILES['picture']['name'],'.'),1)
                    ));
                }
            }
            return $post['eID'];
        }elseif($post['step'] == '3'){
            return $post['eID'];
        }elseif($post['step'] == '4'){
            $data = array(
                'eID'=>$post['eID'],
                'ecName'=>$post['ecName'],
                'ecAddress'=>$post['ecAddress'],
                'ecContactNumber'=>$post['ecContactNumber'],
                'ecRemark'=>$post['ecRemarks']
            );
            if(!isset($post['ecID'])){
                $data['ecID'] = NULL;
                $this->db->insert('employeeemergencycontact',$data);
            }else{
                $this->db->where('ecID',$post['ecID']);
                $this->db->update('employeeemergencycontact',$data);
            }
            return $post['eID'];
        }
    }
    function educ(){
        $post = $this->input->post(NULL,FALSE);
        $data = array(
            'eeLevel'=>$post['level'],
            'eeSchool'=>$post['school'],
            'eeRemarks'=>$post['remarks'],
            'eeStartYear'=>$post['start_year'],
            'eeEndYear'=>$post['end_year']
        );
        $this->load->database();
        if($post['update']=='1'){
            $this->db->where('eeID',$post['eeID']);
            $this->db->delete('employeeeduc');
        }else{
            $data['eeID'] = NULL;
            $data['eID'] = $post['eID'];
            $this->db->insert('employeeeduc',$data);
        }
        header('Content-type:text/json');
        ?>
        {
            <?php 
                if($post['update'] != '1'){
                    echo '"eeID":"'.$this->db->insert_id().'",';
                }
            ?>
            "level": "<?php echo $post['level'];?>",
            "school":"<?php echo $post['school'];?>",
            "remarks":"<?php echo $post['remarks'];?>",
            "start_year":"<?php echo $post['start_year'];?>",
            "end_year":"<?php echo $post['end_year'];?>"
            
        }
         <?php ;
    }
    
    function profile($eID){
        $this->load->library('session');
        $this->load->database();
        if($this->session->userdata('logged_in') !== 'true')
            header('Location: ' .base_url());
        if(!isset($eID)) header('Location: ' .site_url('dashboard'));
        
        $res = $this->db->get_where('employee',array('eID'=>$eID))->result();
        if(!isset($res[0])){
            header('Location: '. site_url('error/profile'));
        }
        
        $data['user'] = $res[0];
        $data['educ'] = $this->db->get_where('employeeeduc',array('eID'=>$res[0]->eID))->result();
        $res2 = $this->db->get_where('employeeemergencycontact',array('eID'=>$res[0]->eID))->result();
        $data['contact'] = $res2[0];
        $res = $this->db->get_where('positions',array('posID'=>$res[0]->ePosition))->result();
        $data['position'] = $res[0]->posName; 
        $data['position_list'] = $this->db->get('positions')->result();
        $this->load->helper('HTML');
        $this->load->view('profile',$data);
    }
    
    function edit($eID){
        
        $this->load->library('session');
        
        if($this->session->userdata('logged_in') !== 'true'){
            header('Location: ' . base_url());
        }
        $data = array();
        $this->load->database();
        $res = $this->db->get_where('employee',array('eID'=>$eID))->result();
        if(!isset($res[0])){
            header('Location: '. site_url('error/profile'));
        }
        
        $data['user'] = $res[0];
        $data['educ'] = $this->db->get_where('employeeeduc',array('eID'=>$res[0]->eID))->result();
        $res2 = $this->db->get_where('employeeemergencycontact',array('eID'=>$res[0]->eID))->result();
        $data['contact'] = $res2[0];
        if($this->input->post('data_on') == 'true'){
            $data['eID'] = $this->_edit_save();
            $data['step'] = $this->input->post('step') + 1;
            $data['post'] = $this->input->post(NULL,FALSE);
        }else{
            $data['step'] = '1';
        }
        if($data['step']=='5'){
            header('Location: ' . site_url('dashboard'));
        }
        $data['positions'] = $this->db->get('positions')->result();
        $this->load->helper('HTML');
        $this->load->view('edit',$data);
    }
    
    function attendance($eID){
        $this->load->library('session');
        $this->load->database();
        $data['vID'] = $this->session->userdata('eID');
        $temp = $this->db->get_where('employee',array('eID'=>$data['vID']))->result();
        $data['vUser'] = $temp[0];
        if($this->session->userdata('logged_in') !== 'true')
            header('Location: ' .base_url());
        if(!isset($eID)) header('Location: ' .site_url('dashboard'));
        
        $res = $this->db->get_where('employee',array('eID'=>$eID))->result();
        if(!isset($res[0])){
            header('Location: '. site_url('error/profile'));
        }
        $post=$this->input->post(NULL,FALSE);
        if(isset($post)){
            $this->_save_attendance();
        }
        
        $data['user'] = $res[0];
        $res = $this->db->get_where('positions',array('posID'=>$res[0]->ePosition))->result();
        $data['position'] = $res[0]->posName; 
        $data['position_list'] = $this->db->get('positions')->result();
        $data['att'] = $this->db->get_where('attendance',array('eID'=>$eID))->result();
        
        $this->load->helper('HTML');
        $this->load->view('attendance',$data);
    }
    
    function _save_attendance(){
        $this->load->database();
        $post=$this->input->post(NULL,FALSE);
        $time = getdate();
        if($post['time_in'] == '1'){
            $data = array(
                "aID"=>NULL,
                "eID"=>$post['eID'],
                "aTimeIn"=>time(),
                "aDate"=>date('Y-m-d'),
                "aRemarks"=>$post['remarks'],
                "aVerified"=>$post['vID']
            );
            $this->db->insert('attendance',$data);
        }else if($post['time_in'] == '0'){
            $this->db->where('aID',$post['aID']);
            $this->db->update('attendance',array(
                'aTimeOut'=>time()
            ));
            $res = $this->db->get_where('attendance',array('aID'=>$post['aID']))->result();
            $time = ($res[0]->aTimeOut - $res[0]->aTimeIn);
            $time = abs($time/3600);
            $this->db->where('aID',$post['aID']);
            $this->db->update('attendance',array(
                'aTotalHours'=>$time
                ));
        }
    }
    
}

?>
